# coding=utf-8
"""
每日8:30
cron: 0 30 8 * * ?
new Env("百度贴吧")
"""
import requests
import utils.db as DB
import utils.tool as TOOL

global msg

msg = """
名称|状态
:--:|:--:
"""
mysql = DB.mysql()


def main():
    dbUser = mysql.select_all("select * from tb_bdtb where status = 0")
    print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉\n")
    print("获取到: "+str(len(dbUser))+" 个用户")
    print("-------------------------------------------")

    for user in dbUser:
        signIn(user)


# 签到
def signIn(user):
    global msg
    url = 'http://c.tieba.baidu.com/c/c/forum/msign'
    data = user['cookie']
    res = requests.post(url=url, data=data)
    json = res.json()

    code = json['error_code']
    if (code == '0'):
        userMsg = json['error']['usermsg']
        if (userMsg == '成功'):
            print(user['user_name'] + " ---> " + "签到成功\n")
            msg += user['user_name'] + "|" + "签到成功" + "\n\n"
        else:
            if (userMsg == "你签得太快了，先看看贴子再来签吧:)"):
                print(user['user_name'] + " ---> " + "今日已签到" +"\n")
                msg += user['user_name'] + "|" + "今日已签到" + "\n\n"
            else:
                print(user['user_name'] + " ---> " + userMsg+"\n")
                msg += user['user_name'] + "|" + userMsg + "\n\n"
    else:
        errMsg = json['error_msg']
        print(user['user_name'] + " ---> " + errMsg +"\n")
        msg += user['user_name'] + "|" + errMsg + "\n\n"
    
    
    print("-------------------------------------------\n")


main()

# 一定要关闭数据库
print("🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉")
mysql.close()

TOOL.msgSend("d9ff2162c1584f6c834204b0bca56c54", " ", msg, True)
